﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using XiAnAirport.BUS;
using XiAnAirport.Models;

namespace XiAnAirport.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class InspectionPlanController : ControllerBase
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        private JwtSettings _jwtSettings;
        public InspectionPlanController(IHostingEnvironment hostingEnvironment, IOptions<JwtSettings> jwtSettingsAccesser)
        {
            _hostingEnvironment = hostingEnvironment;
            _jwtSettings = jwtSettingsAccesser.Value;
        }

        /// <summary>
        /// 新增巡检计划
        /// </summary>
        /// <param name="planinfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddPlan")]
        public ResponseMessage AddPlan(InspectionPlanInfo planinfo)
        {
            InspectionPlanBUS BUS = new InspectionPlanBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                planinfo.preParationTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                message = BUS.AddPlan(planinfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增巡检计划" + planinfo.inspectionNo;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "新增巡检计划失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增巡检计划";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 修改巡检计划
        /// </summary>
        /// <param name="planinfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("EditPlan")]
        public ResponseMessage EditPlan(InspectionPlanInfo planinfo)
        {
            InspectionPlanBUS BUS = new InspectionPlanBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.EditPlan(planinfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改巡检计划" + planinfo.inspectionNo;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "修改巡检计划异常";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改巡检计划";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 载入巡检信息
        /// </summary>
        /// <param name="planinfo">巡检计划信息</param>
        /// <returns>InspectionPlan对象</returns>
        [HttpPost]
        [Route("LoadPlan")]
        public ResponseMessage LoadPlan(InspectionPlanInfo planinfo)
        {
            InspectionPlanBUS BUS = new InspectionPlanBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.LoadPlan(planinfo.id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入巡检信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入巡检计划";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询巡检计划信息
        /// </summary>
        /// <param name="planinfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryPlan")]
        public ResponseMessage QueryPlan(InspectionPlanInfo planinfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                InspectionPlanBUS BUS = new InspectionPlanBUS();
                message = BUS.QueryPlan(planinfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询巡检计划失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询巡检计划";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询巡检历史信息
        /// </summary>
        /// <param name="planinfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryInspectionHistory")]
        public ResponseMessage QueryInspectionHistory(InspectionPlanInfo planinfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                InspectionPlanBUS BUS = new InspectionPlanBUS();
                message = BUS.QueryInspectionHistory(planinfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询巡检历史失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
            }
            return message;
        }

        /// <summary>
        /// 巡检、走修状态列表
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GetWorkOrderStateList")]
        public ResponseMessage GetWorkOrderStateList(InspectionPlanInfo planinfo)
        {
            ResponseMessage message = new ResponseMessage();
            InspectionPlanBUS BUS = new InspectionPlanBUS();
            try
            {
                message = BUS.GetWorkOrderStateList();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "巡检、走修状态获取失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询巡检、走修状态";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除巡检工单
        /// </summary>
        /// <param name="planinfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelPlan")]
        public ResponseMessage DelPlan(InspectionPlanInfo planinfo)
        {
            InspectionPlanBUS BUS = new InspectionPlanBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.DelPlan(planinfo.id);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除巡检工单" + planinfo.inspectionNo;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除巡检工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除巡检工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="inspectionPlanInfo">巡检信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportInspection")]
        public ResponseMessage ExportInspection(InspectionPlanInfo inspectionPlanInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                InspectionPlanBUS bus = new InspectionPlanBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;

                message = bus.ExportPlan(inspectionPlanInfo, sWebRootFolder);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "导出巡检工单";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }

            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出巡检工单";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 生成工单号
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GenerateInspectionNo")]
        public ResponseMessage GenerateInspectionNo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                InspectionPlanBUS bus = new InspectionPlanBUS();
                message = bus.GenerateInspectionNo();
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "生成工单号异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "生成工单号";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 导出巡检历史
        /// </summary>
        /// <param name="inspectionPlanInfo">巡检信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportInspectionHistory")]
        public ResponseMessage ExportInspectionHistory(InspectionPlanInfo inspectionPlanInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                InspectionPlanBUS bus = new InspectionPlanBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;

                message = bus.ExportPlanHistory(inspectionPlanInfo, sWebRootFolder);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "导出巡检历史工单";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }

            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出巡检计划";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 批量删除巡检工单
        /// </summary>
        /// <param name="planinfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BatchDelPlan")]
        public ResponseMessage BatchDelPlan(string ids)
        {
            InspectionPlanBUS BUS = new InspectionPlanBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.BatchDelPlan(ids);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除巡检工单";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "批量删除巡检工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除巡检计划";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
